package com.sunfield.windpower.fragment;

import android.support.v4.view.ViewPager;

import com.sunfield.baseframe.base.BaseFragment;
import com.sunfield.baseframe.base.MyFragmentPagerAdapter;
import com.sunfield.baseframe.http.HttpResult;
import com.sunfield.baseframe.util.JsonUtil;
import com.sunfield.baseframe.util.ScreenUtils;
import com.sunfield.windpower.R;
import com.sunfield.windpower.bean.NewsColumnInfo;
import com.sunfield.windpower.http.HttpClient;
import com.sunfield.windpower.view.MyTabLayout;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.ColorRes;
import org.androidannotations.annotations.res.DimensionPixelOffsetRes;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 首页 门户
 * Created by wxl on 2017/2/6.
 */
@EFragment(R.layout.fragment_hodor)
public class HodorFragment extends BaseFragment {
    //    @ViewsById({R.id.tv_tab_0, R.id.tv_tab_1, R.id.tv_tab_2, R.id.tv_tab_3})
//    List<TextView> tabs;
//    @ViewById
//    ImageView iv_tab_line;
    @ViewById
    ViewPager viewpager;
    @ViewById
    MyTabLayout tab_layout;

    @DimensionPixelOffsetRes(R.dimen.tab_line_width_4)
    int tabWidth;
    int offset;

    @ColorRes(R.color.text_color_blue)
    int tabColorSelected;
    @ColorRes(R.color.text_color_light_gray)
    int tabColorNormal;

    @Bean
    HttpClient http;

    List<BaseFragment> fragmentList;

    String[] titles = {"集团发文", "通知公告", "公司新闻", "项目动态", "其他新闻", "其他通知"};

    @AfterViews
    void init() {
        fragmentList = new ArrayList<>();

//        for (int i = 0; i < 6; i++) {
//            fragmentList.add(NewsListFragment_.builder().index(i).build());
//        }
//        viewpager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), fragmentList));
//        viewpager.setOffscreenPageLimit(fragmentList.size());
//
//        initTab();

        http.getDoorList();
    }

    void initTab() {
        offset = (ScreenUtils.getScreenWidth(getActivity()) / Math.min(fragmentList.size(), 4) - tabWidth) / 2;
        tab_layout.setOffset(offset);
        tab_layout.setLineLength(tabWidth);
        tab_layout.setTabColorNormal(tabColorNormal);
        tab_layout.setTabColorSelected(tabColorSelected);
        tab_layout.setTitleList(Arrays.asList(titles));

        tab_layout.setViewPager(viewpager);
    }

    @Override
    public void onHttpResult(HttpResult result) {
        super.onHttpResult(result);
        if (result.match(http, "getDoorList")) {
            if (result.isSuccess()) {
                List<NewsColumnInfo> columnInfoList = JsonUtil.GetEntityS(JsonUtil.GetJsonArrayByLevel(result.getJson(), "data"), NewsColumnInfo.class);
                for (int i = 0; i < columnInfoList.size(); i++) {
                    NewsColumnInfo columnInfo = columnInfoList.get(i);
                    fragmentList.add(NewsListFragment_.builder().newsColumnId(columnInfo.getId()).index(i).build());
                }

                viewpager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), fragmentList));
                viewpager.setOffscreenPageLimit(fragmentList.size());

                initTab();
            }
        }
    }
}
